package cn.asura.otherChat.util;

import cn.asura.otherChat.server.MyServer;

import java.io.*;
import java.net.Socket;

public class ServerTool {
    public static void createLocalStorage() {
        File file = new File("./storage/server/");
        if (file.exists()) {
            System.out.println("服务器存储的目录已经存在：" + file.getPath());
        } else {
            System.out.println(file.mkdirs() ? "服务器存储的目录创建成功：" + file.getPath() : "服务器存储的目录创建失败：" + file.getPath());
        }
    }

    public static void receiveFile(int id, String fileName, String name) {
        BufferedInputStream bis;
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream("./storage/server/" + fileName));
            bis = new BufferedInputStream(MyServer.sockets[id].getInputStream());

            // 获取文件
            byte[] bytes = new byte[1024];
            int res;
            while ((res = bis.read(bytes)) > 0) {
                bos.write(bytes, 0, res);
                bos.flush();
                if (res < 1024) {
                    break;
                }
            }
            System.out.println(name + " 发送的文件 " + fileName + "已经被保存到服务器上");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void sendFileToClient(int id, File file, String username) {
        BufferedInputStream bis;
        BufferedOutputStream bos;
        PrintStream printStream;

        try {
            printStream = new PrintStream(MyServer.sockets[id].getOutputStream());

            // 判断文件是否存在
            if (file.exists()) {
                // 初始化读写流，从本地目录读取文件，写入到套接字中
                bos = new BufferedOutputStream(MyServer.sockets[id].getOutputStream());
                bis = new BufferedInputStream(new FileInputStream(file));

                // 先发送关键字
                printStream.println("file");

                // 先发送文件名字
                printStream.println(file.getName());

                // 开始读写发送文件本身
                byte[] byteArray = new byte[1024];
                int res;
                while ((res = bis.read(byteArray)) != -1) {
                    bos.write(byteArray, 0, res);
                    bos.flush();
                }

                System.out.println("文件" + file.getName() + "已经成功发送给" + username);

            } else {
                // 发送失败信息
                printStream.println("fail");
                System.out.println("文件" + file.getName() + "发送给" + username + "失败");
            }



        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void shareMsg(int id, String name, String msg) {
        PrintStream printStream;

        // 组装信息并输出到控制台
        msg = name + ": " + msg;
        System.out.println(msg);

        for (int i = 0; i < MyServer.socketNum; i++) {
            if (i == id) {
                continue;
            }
            Socket s = MyServer.sockets[i];
            if (!s.isClosed()) {
                // 使用 printStream 来发送信息
                // 如果关闭 printStream ，socket也会一起被关闭
                try {
                    printStream = new PrintStream(s.getOutputStream());
                    printStream.println(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
